package com.example.leo.mycharmingstudent.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.project.util.SizeUtil;

import java.util.Locale;

/**
 * Created by Leo on 2020/10/13.
 */

public class ProgressBarView extends View {

    //自定义控件所使用的颜色
    private int mColor;
    //线条宽度
    private float mLineWidth;
    //圆的半径
    private float mRadius;
    //进度
    private int mProgress;
    //字体大小
    private float mTextSize;

    //画笔
    private Paint mPaint;

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressBarView);
        mColor = typedArray.getColor(R.styleable.ProgressBarView_pb_color, 0xFFFF0000);
        mLineWidth = typedArray.getDimension(
                R.styleable.ProgressBarView_pb_line_width, 2);
        mRadius = typedArray.getDimension(R.styleable.ProgressBarView_pb_radius, 40);

        mProgress = typedArray.getInt(R.styleable.ProgressBarView_android_progress, 0);
        mTextSize = typedArray.getDimension(R.styleable.ProgressBarView_android_textSize, 40);

        //资源回收
        typedArray.recycle();

        initPaint();
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint();
        //设置画笔颜色
        mPaint.setColor(mColor);
        //设置抗锯齿
        mPaint.setAntiAlias(true);
        //设置画笔样式
        mPaint.setStyle(Paint.Style.STROKE);
    }

    /**
     * 测量自身大小
     * MeasureSpec三种模式含义
     * MeasureSpec.EXACTLY：精确值
     * MeasureSpec.AT_MOST：最大值
     * MeasureSpec.UNSPECIFIED：不确定值,不限制
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            int needWidth = measureWidth() + getPaddingLeft() + getPaddingRight();
            if (widthMode == MeasureSpec.AT_MOST) {
                //Math.min 返回较小的值
                width = Math.min(needWidth, widthSize);
            } else {
//                MeasureSpec.UNSPECIFIED
                width = needWidth;
            }
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            int needHeight = measureHeight() + getPaddingTop() + getPaddingBottom();
            if (heightMode == MeasureSpec.AT_MOST) {
                height = Math.min(needHeight, heightSize);
            } else {
//                MeasureSpec.UNSPECIFIED
                height = needHeight;
            }
        }

        //设置测量完成后的尺寸，不调用会导致没尺寸，不显示
        setMeasuredDimension(width, height);

    }

    private int measureHeight() {
        return (int) (mRadius * 2);
    }

    private int measureWidth() {
        return (int) (mRadius * 2);
    }

    /**
     * 绘制控件
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        //根据需求，统共分三步
        //第一步：画圆

        mPaint.setStrokeWidth(mLineWidth / 4.0f);
        /*
        此处使用mRadius需要格外注意：
        mRadius的值不能超过控件宽度减去左右padding值
         */
        canvas.drawCircle(width / 2, height / 2, width / 2 - getPaddingLeft() - mPaint.getStrokeWidth() / 2, mPaint);

        //第二步：画圆弧
        mPaint.setStrokeWidth(mLineWidth);
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingRight());
        float angle = mProgress / 100.0f * 360;
        canvas.drawArc(
                new RectF(0, 0,
                        width - getPaddingLeft() - getPaddingRight(),
                        height - getPaddingTop() - getPaddingBottom()),
                0, angle,
                false,
                mPaint);
        canvas.restore();
        //第三步：画字
        String text = String.format(Locale.CHINA, "%d%%", mProgress);
        mPaint.setStrokeWidth(0);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setTextSize(mTextSize);
        Rect bound = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), bound);
        int textHeight = bound.height();
        canvas.drawText(text, 0, text.length(), width / 2, height / 2 + textHeight / 2, mPaint);
    }

    public void setProgress1(int progress) {
        this.mProgress = progress;
        /*
        重新绘制，告诉底层重新绘制一下控件，也就是调用onDraw
        手动调用onDraw方法，不会重新绘制
         */
        invalidate();
        /*
        invalidate();在当前线程执行
        postInvalidate();保证在主线程执行
         */

    }

    /**
     * 从ProgressBarView中获取全局变量progress的值
     * @return
     */
    public int getProgress() {
        return this.mProgress;
    }
}
